package com.hycreate.platform.modules.config;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.modules.config.SfConfig;
import com.hycreate.platform.modules.config.SfConfigService;

/**
 * 前端配置Controller
 * @author hoader
 * @version 2017-01-17
 */
@Controller
@RequestMapping(value = "${adminPath}/config/sfConfig")
public class SfConfigController extends BaseController {

	@Autowired
	private SfConfigService sfConfigService;
	
	@ModelAttribute
	public SfConfig get(@RequestParam(required=false) String id) {
		SfConfig entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sfConfigService.get(id);
		}
		if (entity == null){
			entity = new SfConfig();
		}
		return entity;
	}
	
	@RequiresPermissions("config:sfConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(SfConfig sfConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SfConfig> page = sfConfigService.findPage(new Page<SfConfig>(request, response), sfConfig); 
		model.addAttribute("page", page);
		return "modules/config/sfConfigList";
	}

	@RequiresPermissions("config:sfConfig:view")
	@RequestMapping(value = "form")
	public String form(SfConfig sfConfig, Model model) {
		List<SfConfig> sfConfigList = sfConfigService.findList(sfConfig);
		if(CollectionUtils.isNotEmpty(sfConfigList) && sfConfigList.size() > 0){
			sfConfig = sfConfigList.get(0);
		}else{
			sfConfig = new SfConfig();
		}
		model.addAttribute("sfConfig", sfConfig);
		return "modules/config/sfConfigForm";
	}

	@RequiresPermissions("config:sfConfig:edit")
	@RequestMapping(value = "save")
	public String save(SfConfig sfConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sfConfig)){
			return form(sfConfig, model);
		}
		sfConfig.setStatus("1");
		sfConfigService.save(sfConfig);
		addMessage(redirectAttributes, "保存前端配置成功");
		return "redirect:"+Global.getAdminPath()+"/config/sfConfig/form";
//		return "redirect:"+Global.getAdminPath()+"/config/sfConfig/?repage";
	}
	
	@RequiresPermissions("config:sfConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(SfConfig sfConfig, RedirectAttributes redirectAttributes) {
		sfConfigService.delete(sfConfig);
		addMessage(redirectAttributes, "删除前端配置成功");
		return "redirect:"+Global.getAdminPath()+"/config/sfConfig/?repage";
	}

}